mod names;
mod ports;
mod byserialrs;
#[derive(Default,PartialEq)]
pub struct Comport{
    pub com:String,
    pub manufacturer:String,
    pub product:String,
}
pub fn available_ports()->Vec<Comport>{
    // for idx in names::available_ports(vec!["CNCPorts","Ports"])
    let mut comports = Vec::new();
    let mut port = byserialrs::port_lister::COMPortLister{};
    if let Ok(ports) = port.list_devices(){
        for idx in ports{
            comports.push(
                Comport{
                    com:idx.get_port().into(),
                    manufacturer:idx.get_manufacturer().into(),
                    product:idx.get_desc().into()
                }
            )
        }
    }
    
    for idx in names::available_ports(vec!["CNCPorts"]){
        let mut temp = Comport::default();
        if let Some(ss) = idx.serial_number{
            temp.com = ss;
        }else{
            continue;
        }
        if let Some(ss) = idx.product {
            temp.product = ss;
        }
        if let Some(ss) = idx.manufacturer{
            temp.manufacturer = ss;
        }
        comports.push(temp);
    }
    comports
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test] 
    fn it_works() {
        let mut cc =  available_ports();
        // cc.sort_by_key(|k| k.com.clone());
        println!("{}",cc.len());
            for idx in cc{
                println!("{}", idx.com);
                
                println!("{}",idx.manufacturer);
                
                println!("{}",idx.product);
            }
    }
    #[test]
    fn test1(){
        // use super::byserialrs::
        let mut dd = super::byserialrs::port_lister::COMPortLister{};
        if let Ok(d) = dd.list_devices(){
            for idx in d{
                println!("{}",idx.get_port());
                println!("{}",idx.get_manufacturer());
                println!("{}",idx.get_desc());
            }
        }
    }
}